﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_Product_Search
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Not Page.RouteData.Values("CateId") Is Nothing Then
                hidCateId.Value = Page.RouteData.Values("CateId")
                getNhanHieu()
                getDoTuoi()
                ShowData()
            End If
        End If
    End Sub
    Protected Sub getNhanHieu()
        ddlNhanHieu.Items.Clear()
        ddlNhanHieu.AppendDataBoundItems = True
        ddlNhanHieu.Items.Add(New ListItem("---Chọn---", "0"))
        Using data As New VSEntities
            Dim p = (From q In data.ProductUnits Order By q.ProductUnitName Select q).ToList
            ddlNhanHieu.DataValueField = "ProductUnitID"
            ddlNhanHieu.DataTextField = "ProductUnitName"
            ddlNhanHieu.DataSource = p
            ddlNhanHieu.DataBind()
            ddlNhanHieu.SelectedValue = 0
        End Using

    End Sub
    Protected Sub getDoTuoi()
        ddlDoTuoi.Items.Clear()
        ddlDoTuoi.AppendDataBoundItems = True
        ddlDoTuoi.Items.Add(New ListItem("---Chọn---", "0"))
        Using data As New VSEntities
            Dim p = (From q In data.ProDoTuois Order By q.Title Select q).ToList
            ddlDoTuoi.DataValueField = "Id"
            ddlDoTuoi.DataTextField = "Title"
            ddlDoTuoi.DataSource = p
            ddlDoTuoi.DataBind()
            ddlDoTuoi.SelectedValue = 0
        End Using

    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _lstColor = (From q In data.ProColors Select q Order By q.Title).ToList
            With rptColor
                .DataSource = _lstColor
                .DataBind()
            End With
            'lay thong tin size
            Dim _lstSize = (From q In data.ProSizes Select q Order By q.Title).ToList
            With rptSize
                .DataSource = _lstSize
                .DataBind()
            End With
        End Using
    End Sub

    Protected Sub rptColor_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptColor.ItemDataBound
        Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
        Dim proc = CType(e.Item.DataItem, ProColor)
        Dim ltrColor As Literal = CType(e.Item.FindControl("ltrColor"), Literal)
        ltrColor.Text = "<div class='Color' style='background:" & proc.MaMau & "'><a href='" & MainDomain & RootCateId.ToString & "/" & "mau-sac-" & proc.Id & ".html'>" & "&nbsp;</a></div>"
    End Sub

    Protected Sub rptSize_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptSize.ItemDataBound
        Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
        Dim pros = CType(e.Item.DataItem, ProSize)
        Dim ltrSize As Literal = CType(e.Item.FindControl("ltrSize"), Literal)
        ltrSize.Text = "<div class='Size' ><a href='" & MainDomain & RootCateId.ToString & "/" & "kich-co-" & pros.Id & ".html'>" & pros.Title & "</a></div>"
    End Sub

    Protected Sub ddlNhanHieu_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlNhanHieu.SelectedIndexChanged
        Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
        Dim proThuongHieu As New ProductUnit
        Dim url As String = MainDomain & RootCateId.ToString & "/thuong-hieu-" & ddlNhanHieu.SelectedValue & ".html"
        Response.Redirect(url)
    End Sub
    'lay nhanh goc cua category theo catid truyen vao
    Protected Function getRootMenu(ByVal _catId As Integer) As Integer
        Dim _hientaiMenuId = _catId
        Using Data As New VSEntities
            Dim p = (From q In Data.Categories Where q.CatId = _hientaiMenuId Select q).FirstOrDefault
            If p Is Nothing Or p.ParentId = 0 Then
                Return _hientaiMenuId
            Else
                Return getRootMenu(p.ParentId)
            End If
        End Using
        Return _hientaiMenuId
    End Function

    Protected Sub ddlDoTuoi_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddlDoTuoi.SelectedIndexChanged
        Dim RootCateId As Integer = getRootMenu(hidCateId.Value)
        Dim proDoTuoi As New ProDoTuoi
        Dim url As String = MainDomain & RootCateId.ToString & "/Do-Tuoi-" & ddlDoTuoi.SelectedValue & ".html"
        Response.Redirect(url)
    End Sub
End Class
